package com.线程案例;

import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author: LCG
 * 作者: LCG
 * 日期: 2021/2/26  8:10
 * 描述:
 */
public class TestThreadDemo {

    public static void main(String[] args) throws InterruptedException, ExecutionException {

        Long strat=System.currentTimeMillis();

        List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();

        for(int i=0;i<40000;i++){
            Map<String,Object> map=new HashMap<String,Object>();
            map.put("goodsId",i);
            map.put("money",new Random().nextDouble());
            map.put("mount",new Random().nextInt(10000));
            list.add(map);
        }

        /*for (Map<String, Object> map : list) {
            map.put("goodsName",new Random().nextDouble()+"商品名称" );
            map.put("brandName",new Random().nextDouble()+"品牌名称" );
            map.put("goodsStyle",new Random().nextDouble()+"商品规格" );
            map.put("goodsCode",new Random().nextDouble()+"商品编码" );
            map.put("goodsName",new Random().nextDouble()+"商品名称" );
            map.put("XXXX",new Random().nextDouble()+"商品名称" );
            map.put("YYYY",new Random().nextDouble()+"商品名称" );
            map.put("TTTT",new Random().nextDouble()+"商品名称" );
            map.put("UUUU",new Random().nextDouble()+"商品名称" );
        }*/

        FutureTask<List<Map<String,Object>>> task1=new FutureTask<>(new MyGoodsRun(list.subList(0,10000)));
        FutureTask<List<Map<String,Object>>> task2=new FutureTask<>(new MyGoodsRun(list.subList(10000,20000)));
        FutureTask<List<Map<String,Object>>> task3=new FutureTask<>(new MyGoodsRun(list.subList(20000,30000)));
        FutureTask<List<Map<String,Object>>> task4=new FutureTask<>(new MyGoodsRun(list.subList(30000,40000)));

        Thread thread1=new Thread(task1);
        Thread thread2=new Thread(task2);
        Thread thread3=new Thread(task3);
        Thread thread4=new Thread(task4);

        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();

        thread1.join();
        thread2.join();
        thread3.join();
        thread4.join();

        List<Map<String,Object>> list2=new ArrayList<Map<String,Object>>();

        list2.addAll(task1.get());
        list2.addAll(task2.get());
        list2.addAll(task3.get());
        list2.addAll(task4.get());

        Long end =System.currentTimeMillis();

        System.out.println(list2.size()+"======="+(end-strat));
    }

}
